/**
 * Created by liuqingling on 16/6/14.
 */
define(['../components/loading','jquery','./md5', './CommonTools'],function(loading,$,MD5,CommonTools){
    $.ajaxSetup({
        xhrFields: {
            withCredentials: true
        },
        beforeSend:function(){
            loading.showLoading();
        },

        dataType: 'json'
    });

    return {
        extend:function (des, src, override){
        for( var i in src){
            if(override || !(i in des)){
                des[i] = src[i];
            }
        }
        return des;
        },
        initPostParams:function(path,newParams){
            var params={
                timestamp:(new Date).valueOf(),
                v:window.app.ApiConfig.apiVersion,
                token:window.app.ApiConfig.token
            };
            var clone_params={
                timestamp:params.timestamp,
                v:params.v,
                token:params.token
            };
            if(newParams){
                this.extend(params,newParams);
            }
            var paramsString='';
            var keys = Object.keys(params);
            keys.sort();
            keys.forEach(function(it,index){
                if(index==keys.length-1){
                    paramsString+=it+'='+params[it];
                }else {
                    paramsString+=it+'='+params[it]+'&';
                }
            });
            var sign = MD5.createHash(paramsString + window.app.ApiConfig.salt);
            //另外原来的
            //newParams.tem_res=old_tem_res
            var old_paramsString='';
            var old_keys = Object.keys(clone_params);
            old_keys.sort();
            old_keys.forEach(function(it,index){
                if(index==old_keys.length-1){
                    old_paramsString+=it+'='+clone_params[it];
                }else {
                    old_paramsString+=it+'='+clone_params[it]+'&';
                }
            });
            old_paramsString = old_paramsString+'&sign='+sign;
            return old_paramsString;
        },
        initParams:function(path,newParams){
            //var token='acff958015c25f68a79606014b1f1f678c9613fe';
            //var sn = '5760fe486246e147464129';
            //var baseUrl='http://h5.api.esn.ren:6061';
            //var apiVersion='1.0';
           // var path='/diary/getDailyMemberList'//接口path
            var params={
                timestamp:(new Date).valueOf(),
                v:window.app.ApiConfig.apiVersion,
                token:window.app.ApiConfig.token
            };
            if(newParams){
                params=this.extend(params,newParams);
            }
            console.log(params);
            var paramsString='';
            var keys = Object.keys(params);
            keys.sort();
            keys.forEach(function(it,index){
                if(index==keys.length-1){
                    paramsString+=it+'='+params[it];
                }else {
                    paramsString+=it+'='+params[it]+'&';
                }
            });
            //console.log(window.app.ApiConfig.sn+path+paramsString+window.app.ApiConfig.salt);
            var sign = MD5.createHash(paramsString + window.app.ApiConfig.salt);
            //console.log(sign);

            paramsString = paramsString+'&sign='+sign;
            return paramsString;
        },
        /**
         *
         * @param op
         * {
         *      path:''//比如localhost:300/abcd  path为/abcd
         *      params:{} 参数对象
         *      success:function(data){}成功回调
         * }
         */
        ajaxGet:function(op){
            if(!op.error){
                op.error=function(err){
                    console.log(err);
                }
            }
            $.ajax({
                type: 'GET',
                url: window.app.ApiConfig.baseUrl+op.path,
                    data:this.initParams(op.path,op.params),
                success: function(data){
                    console.log(data);
                    loading.hideLoading();
                    if(data.code===0){
                        op.success&&op.success(data.data);
                    }else {
                        CommonTools.Toast(data.msg);
                        // op.dataEmpty&&op.dataEmpty(data);
                    }
                },
                error:function(){
                    loading.hideLoading();
                    op.error&&op.error
                }
            });
        },
        /**
         *
         * @param op
         * {
         *      path:''//比如localhost:300/abcd  path为/abcd
         *      params:{} 参数对象
         *      success:function(data){}成功回调
         * }
         */
        ajaxPost:function(op){
            if(!op.error){
                op.error=function(err){
                    console.log(err);
                }
            }
            //this.initPostParams(op.path,op.params);
            var uri=window.app.ApiConfig.baseUrl+op.path+"?"+this.initPostParams(op.path,op.params);

            //console.log('url 完整的!!!!!!!');
            //console.log(uri);
            //console.log('data上面的参数');
            //console.log(op.params);
            $.ajax({
                type: 'POST',
                url: uri,
                data:op.params?op.params:'',
                success: function(data){
                    console.log(data);
                    loading.hideLoading();
                    if(data.code===0){
                        op.success&&op.success(data.data);
                    }else {
                        CommonTools.Toast(data.msg);
                        // op.dataEmpty&&op.dataEmpty(data);
                    }
                },
                error:function(){
                    loading.hideLoading();
                    op.error&&op.error();
                }
            });
        },
        ajaxPostDeferred:function (opt) {
            var uri = window.app.ApiConfig.baseUrl+opt.path + "?" + this.initPostParams(opt.path,opt.params);
            var defer = $.Deferred();
            var jqXHR = $.ajax({
                url: uri,
                data: opt.params? opt.params:'',
                type: 'post',
                dataType: 'json',
                success: function(d, textStatus, xhr) {
                    // 成功
                    loading.hideLoading();
                    if (d.code === 0) {
                        defer.resolve(d.data);
                        return;
                    } else {
                        defer.reject(d);
                    }

                },
                error: function(d) {
                    defer.reject(d);
                }
            });
            if (opt.success) defer.done(opt.success);
            if (opt.error) defer.fail(opt.error);
            return defer;
        },
        ajaxGetDeferred:function (opt) {
            var defer = $.Deferred();
            var jqXHR = $.ajax({
                url: window.app.ApiConfig.baseUrl + opt.path,
                data: this.initParams(opt.path, opt.params),
                type: 'get',
                dataType: 'json',

                success: function(d, textStatus, xhr) {
                    // 成功
                    loading.hideLoading();

                    if (d.code === 0) {
                        defer.resolve(d.data);
                        return;
                    } else {
                        defer.reject(d);
                    }

                },
                error: function(d) {
                    loading.hideLoading();
                    defer.reject(d);
                }
            });
            if (opt.success) defer.done(opt.success);
            if (opt.error) defer.fail(opt.error);
            return defer;
        }
        
        
    }
});
